<template>
	<view class="content">
		<button class="com-btn" @click="wxAuth">微信快捷登录</button>
		<!-- <button class="com-btn" @click="goBack">手动跳转</button> -->
	</view>
</template>

<script setup>
	import { getWxAccess } from '@/api/login.js'
	const data = reactive({
		path: ''
	})
	
	const wxAuth = ()=>{
		const fullPath = 'https://h5.whalesaas.com/pages/index'
		// const fullPath = 'https://h5-test.whalesaas.com/pages/index'
		let wx_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxfd29603083857ed5&redirect_uri=' + fullPath + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
		location.href = wx_url;
	}
	onLoad(() => {
		const params = new URLSearchParams(window.location.search)
		let wxCode = params.get('code')
		if(wxCode){
			wechatLogin()
		}else{
			uni.setStorageSync('orgin', params.get('orgin'))
			uni.setStorageSync('liveNo', params.get('live_no'))
			uni.setStorageSync('companyCode', params.get('company_code'))
			uni.setStorageSync('mid', params.get('mid'))
		}
	})
	const wechatLogin = ()=> {
		uni.showLoading();
		const search = new URLSearchParams(window.location.search)
		const code = search.get('code')
		const params = {
			code: code,
			company_code: uni.getStorageSync('companyCode'),
			mid: uni.getStorageSync('mid'),
		}
		getWxAccess(params).then(res=>{
			uni.hideLoading();
			if(res.code == 200001){
				const openid = res.data.openid
				const companyCode = uni.getStorageSync('companyCode')
				const liveNo = uni.getStorageSync('liveNo')
				const mid = uni.getStorageSync('mid')
				const source = uni.getStorageSync('source')
				const orgin = uni.getStorageSync('orgin')
				let path = ''
				if(liveNo){
					path = `${orgin}?live_no=${liveNo}&company_code=${companyCode}&mid=${mid}&openid=${openid}`
				}else{
					path = `${orgin}?company_code=${companyCode}&mid=${mid}&openid=${openid}`
				}
				// data.path = []
				window.location.href = path
			}else{
				uni.showToast({
					title: res.message,
					icon:'none',
					duration: 2000
				})
			}
		})
	}
	const goBack = () => {
		window.location.href = path
	}
</script>

<style lang="scss" scoped>
	page{background: #f1f1f1;}
	.com-btn{margin-top: 30vh;margin-left: 50px;margin-right: 50px;}
</style>

